package Day23.日期相关类;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author hyhWTX
 * @version 1.0
 * @date 2022年04月10日 8:59
 */


public class CalendarDemo1 {
    public static void main(String[] args) {
        //获取Calendar对象
        Calendar calendar = Calendar.getInstance();
        //获得当前时间点的date
        Date time = calendar.getTime();
        String nowTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(time);
        System.out.println(nowTime);
        String noeTime1 = getTimeStr(calendar);
        System.out.println(noeTime1);
    }

    private static String getTimeStr(Calendar calendar) {
        /**
         * 根据日历对象获得一个时间字符串
         * @param calendar
         */
        //获得年
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR);
        int min = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        String nowTime = year+"-"+fillZero(month)+"-"+fillZero(day)+" "+fillZero(hour)+":"+fillZero(min)+":"+fillZero(second);
        return nowTime;
    }

    private static String fillZero(int num) {
        if (num<10){
            return "0"+num;
        }
        return num+" ";
    }
}
